﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Deathknight
{
    public class BloodTank : TalentBase
    {
        public override string Name
        {
            get { return "Blood 1.41"; }
        }

        public override float CombatMaxDistance
        {
            get
            {
                return 5f;
            }
        }

        public override string KeySpell
        {
            get { return "Heart Strike"; }
        }

        public override Composite SingleRotation
        {
            get
            {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Blood Fury", a => true, "Blood Fury"),
                    SC.CastSpell("Lifeblood", a => true, "Lifeblood"),
                    SC.CastSpell("Berserking", a => true, "Berserking"),
                    SC.CastSpell("Rune Tap", a => SC.PlayerHasBuff("Will of the Necropolis") || Me.HealthPercent < 90, "Rune Tap"),
                    SC.CastSpell("Blood Tap", a => Me.BloodRuneCount > 0 && (Me.FrostRuneCount < 1 || Me.UnholyRuneCount < 1), "Blood Tap"),
                    SC.CastSpell("Raise Dead", a => (Me.Pet == null || Me.Pet.Dead) && Me.HealthPercent < 50, "Raise Dead"),
                    SC.CastSpell("Death Pact", a => SC.SpellCooldown("Raise Dead") > 121 && Me.HealthPercent < 60, "Death Pact"),
                    SC.RunMacroText("/Cast [@Player] Death Coil", a => SC.CanCast("Death Coil") && SC.PlayerHasBuff("Lichborne") && Me.HealthPercent < 90, "Death Coil"),
                    SC.CastSpell("Death Strike", a => Me.HealthPercent < 60, "Death Strike"),
                    SC.CastSpell("Dancing Rune Weapon", a => true, "Dancing Rune Weapon"),
                    SC.CastSpell("Outbreak", a => !SC.TargetHasDebuff("Blood Plague") || !SC.TargetHasDebuff("Frost Fever"), "Outbreak"),
                    SC.CastSpell("Pestilence", a => SC.CountEnnemiesInRange(Me.CurrentTarget.Location, 10) > 1 && SC.TargetHasDebuff("Blood Plague") && SC.TargetHasDebuff("Frost Fever") && SC.SpellCooldown("Outbreak") > 28, "Pestilence"),
                    new Decorator(
                        ret => Me.GotTarget && SC.CanCast("Death and Decay") && SC.CountEnnemiesInRange(Me.CurrentTarget.Location, 18) > 3 && (Me.UnholyRuneCount > 1 || Me.DeathRuneCount > 1),
                        new Sequence(
                            new TreeSharp.Action(ret => SC.CastSpell("Death and Decay")),
                            new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(0, Me.CombatReach+0.33333f + Me.CurrentTarget.CombatReach) - 3f)))
                        )
                    ),
                    SC.CastSpell("Plague Strike", a => !SC.TargetHasDebuff("Blood Plague") && (Me.UnholyRuneCount > 1 || Me.DeathRuneCount > 2), "Plague Strike"),
                    SC.CastSpell("Icy Touch", a => !SC.TargetHasDebuff("Frost Fever") && (Me.FrostRuneCount > 1 || Me.DeathRuneCount > 2), "Icy Touch"),
                    SC.CastSpell("Death Strike", a => Me.HealthPercent < 90 || !SC.PlayerHasBuff("Blood Shield"), "Death Strike"),
                    SC.CastSpell("Blood Boil", a => SC.PlayerHasBuff("Crimson Scourge"), "Blood Boil"),
                    SC.CastSpell("Death Strike", a => true, "Death Strike"),
                    SC.CastSpell("Heart Strike", a => SC.CountEnnemiesInRange(Me.Location, 14) > 1 && Me.HealthPercent > 90 && SC.PlayerHasBuff("Blood Shield"), "Heart Strike"),
                    SC.CastSpell("Blood Boil", a => SC.CountEnnemiesInRange(Me.Location, 14) > 3 && Me.HealthPercent > 90 && SC.PlayerHasBuff("Blood Shield"), "Boil Blood"),
                    SC.CastSpell("Heart Strike", a => Me.BloodRuneCount > 1 || Me.DeathRuneCount > 2, "Heart Strike"),
                    SC.CastSpell("Rune Strike", a => !SC.PlayerHasBuff("Lichborne") && Me.CurrentRunicPower >= 60, "Rune Strike"),
                    SC.CastSpell("Empower Rune Weapon", a => Me.DeathRuneCount < 2 && Me.BloodRuneCount < 1 && Me.UnholyRuneCount < 1 && Me.FrostRuneCount < 1, "Empower Rune Weapon"),
                    SC.CastSpell("Horn of Winter", a => true, "Horn of Winter")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastSpell("Bone Shield", a => !Me.Mounted && !SC.PlayerHasBuff("Bone Shield"), "Bone Shield"),
                    SC.CastBuff("Horn of Winter", a => !Me.Mounted && !Me.Combat, "Horn of Winter")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get { return SingleRotation; }
        }
    }
}
